package com.yunkeji.config.pay;

import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.WxPayApiConfigKit;
import com.yunkeji.config.properties.PayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * 支付配置
 *
 * @author 老张
 */
@Configuration
public class PayConfig {
    @Autowired PayProperties payProperties;

    /**
     * 初始化支付宝配置
     *
     * @author 老张
     */
    @PostConstruct
    public void initAliPay() {
        PayProperties.Ali ali = payProperties.getAli();
        AliPayApiConfig aliPayApiConfig =
                AliPayApiConfig.builder()
                        .setAppId(ali.getAppid())
                        .setAliPayPublicKey(ali.getAliPayPublicKey())
                        // 下面三个参数,证书模式使用,退款需要证书
                        // .setAppCertPath("")
                        // .setAliPayCertPath("")
                        //   .setAliPayRootCertPath("")
                        .setCharset("UTF-8")
                        .setPrivateKey(ali.getPrivateKey())
                        .setServiceUrl(ali.getServiceUrl())
                        .setSignType("RSA2")
                        // 普通公钥方式
                        .build();
        // 证书模式
        // .buildByCert();
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayApiConfig);
        //存储第二个支付配置
        AliPayApiConfig aliPayApiConfig_2 =
            AliPayApiConfig.builder()
                .setAppId(ali.getAppidTwo())
                .setAliPayPublicKey(ali.getAliPayPublicKeyTwo())
                // 下面三个参数,证书模式使用,退款需要证书
                // .setAppCertPath("")
                // .setAliPayCertPath("")
                //   .setAliPayRootCertPath("")
                .setCharset("UTF-8")
                .setPrivateKey(ali.getPrivateKeyTwo())
                .setServiceUrl(ali.getServiceUrl())
                .setSignType("RSA2")
                // 普通公钥方式
                .build();
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayApiConfig_2);

    }

    /**
     * 初始化微信支付配置
     *
     * @author 老张
     */
    @PostConstruct
    public void initWxPay() {
        PayProperties.Wx wx = payProperties.getWx();
        WxPayApiConfig apiConfig =
                WxPayApiConfig.builder()
                        .appId(wx.getAppid())
                        .mchId(wx.getMchId())
                        .partnerKey(wx.getPartnerKey())
                        .certPath(wx.getCertPath())
                        .domain(wx.getNotifyUrl())
                        .build();
        WxPayApiConfigKit.setThreadLocalWxPayApiConfig(apiConfig);
    }
}
